package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.BusinessCardOrder;
import com.sc.nft.entity.vo.BusinessCardOrderAdminVO;
import com.sc.nft.entity.vo.UserBusinessCardOrderPageVO;
import com.sc.nft.enums.OrderStatusEnum;
import com.sc.nft.mapper.BusinessCardOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class BusinessCardOrderDao extends ServiceImpl<BusinessCardOrderMapper, BusinessCardOrder> {
    /**
     * 获取用户是否存在未支付的订单
     *
     * @param userId
     * @param orderStatusEnum
     * @return
     */
    public List<BusinessCardOrder> getUserHasAnyUnpaidOrders(Long userId, OrderStatusEnum orderStatusEnum) {
        LambdaQueryWrapper<BusinessCardOrder> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BusinessCardOrder::getUserId, userId);
        wrapper.eq(BusinessCardOrder::getStatus, orderStatusEnum);
        wrapper.gt(BusinessCardOrder::getCloseTime, DateUtil.date());
        return list(wrapper);
    }

    /**
     * 获取用户指定状态的订单
     *
     * @param userId
     * @param orderStatusEnum
     * @return
     */
    public List<BusinessCardOrder> getUserIdByStatus(Long userId, OrderStatusEnum orderStatusEnum) {
        LambdaQueryWrapper<BusinessCardOrder> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BusinessCardOrder::getUserId, userId);
        wrapper.eq(BusinessCardOrder::getStatus, orderStatusEnum);
        return list(wrapper);
    }

    public BusinessCardOrder getByOrderNo(String orderNo) {
        LambdaQueryWrapper<BusinessCardOrder> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BusinessCardOrder::getOrderNo, orderNo);
        wrapper.orderByDesc(BusinessCardOrder::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public Page<UserBusinessCardOrderPageVO> userBusinessCardOrderPage(Page mp, Long userId) {
        return baseMapper.userBusinessCardOrderPage(mp, userId);
    }

    public Page<BusinessCardOrderAdminVO> adminBusinessCardOrderPage(Page page,
                                                                     String orderNo,
                                                                     String userTel,
                                                                     String orderStartTime,
                                                                     String orderEndTime,
                                                                     String payStartTime,
                                                                     String payEndTime) {
        return baseMapper.adminBusinessCardOrderPage(page, orderNo, userTel, orderStartTime, orderEndTime, payStartTime, payEndTime);

    }

    public Boolean closeOrder(Long id) {
        LambdaUpdateWrapper<BusinessCardOrder> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(BusinessCardOrder::getId, id);
        wrapper.set(BusinessCardOrder::getStatus, OrderStatusEnum.CLOSE);
        return update(wrapper);
    }

    public List<BusinessCardOrder> getCloseOrder() {
        LambdaQueryWrapper<BusinessCardOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.lt(BusinessCardOrder::getCloseTime, DateTime.now());
        wrapper.eq(BusinessCardOrder::getStatus, OrderStatusEnum.WAIT_PAY);
        return list(wrapper);

    }


    public List<BusinessCardOrder> getWaitSettleOrder() {
        LambdaQueryWrapper<BusinessCardOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BusinessCardOrder::getStatus, OrderStatusEnum.PAYMENT);
        wrapper.eq(BusinessCardOrder::getSettleFlag, false);
        return list(wrapper);

    }
}
